<template>
    <div class="category">
        <header class="category-header" v-if="false">
            歌曲分类
        </header>
        <nav class="category-nav">
            <div class="category-nav-box">
                <div  v-for="(item, i) in navArr" 
                :key="item.category_id"
                :class="num == i ? 'opt active' : 'opt'"
                    @click="addCategoryFunc(item.category_name, i)">{{ item.category_name }}</div>
            </div>
        </nav>
        <section class="song-list">
            <div v-if="arr.length == 0">
                <van-empty description="暂无分类列表" />
            </div>
            <!-- 搜索列表 -->
            <ul class="song-list-1" v-else>
                <li v-for="(item) in arr" :key="item.id">
                    <router-link :to="{ path: '/detail', query: { song_id: item.song_id , ui:'category',category_name: category_name} }">
                        <div class="left">
                            <div class="img-box"><img v-if="item.song_cover_url" :src="item.song_cover_url" alt=""></div>
                           <div>
                                <h5>{{ item.song_name }}</h5>
                                <div v-if="item.singer_name">
                                    <span >{{ item?.singer_name }} </span> | 
                                    <span >{{ item?.category_name }} </span>
                                </div>
                           </div>
                        </div>
                        <div class="right">
                            <button>
                                <van-icon name="play-circle-o" />
                            </button>
                        </div>
                    </router-link>
                </li>
            </ul>

        </section>
    </div>
</template>

<script>
import { getCategoryList } from '../api/index';
export default {
    data() {
        return {
            num: 0,
            navArr: [],
            songs: [],
            arr: [],
            category_name: ""
        }
    },
    created() {
        this.getCategoryListFn();
    },
    methods: {
        async addCategoryFunc(category_name, index=0) {
            this.num = index;
            this.category_name = category_name;
            await this.$store.dispatch('music/addCategoryFn', { category_name: category_name });
            this.arr = this.$store.state.music.songs;
        },
        async getCategoryListFn() {
            await getCategoryList().then(content => {
                let { result, msg } = content;
                if (result) {
                    console.log(result[0].category_name)
                    this.navArr = result;
                    // 初始化
                    this.addCategoryFunc(result[0].category_name);
                    this.category_name = result[0].category_name;
                }
                else {
                    console.log(msg);
                }
            }).catch(err => console.log(err))
        },
    }
}
</script>



<style lang="less">
@import '../common/less/varible.less';

.category {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 18px;
}

.category-header {
    font-weight: bold;
    padding: 15px 15px;
    background-color: @bgColor;
    color: #fff;
}

.category-nav {
    width: 100%;
    overflow: scroll;
    background-color: #f0f0f0;

    .category-nav-box {
        display: flex;
        align-items: center;
        height: 60px;
        flex-wrap: nowrap;
        width: 100%;

        .opt {
            flex-shrink: 0;
            height: 40px;
            line-height: 40px;
            padding: 5px 10px;

            &.active {
                color: @activeColor;
            }
        }
    }
}

::-webkit-scrollbar {
    display: none;
}

.song-list {
    padding: 10px 15px;
    ul.song-list-1 {
        padding: 0 15px;
        margin-top: 10px;
        li {
            a {
                display: flex;
                width: 100%;
                justify-content: space-between;
                align-items: center;
                font-family: Arial, Helvetica, sans-serif;
                padding: 10px 0px;
                border-bottom: 1px solid #f0f0f0;
                .left {
                    display: flex;
                    .img-box {
                        width: 80px;
                        margin-right: 10px;
                        img {
                            width: 100%;
                        }
                    }
                    h5 {
                        font-weight: normal;
                        font-size: 18px;
                        line-height: 175%;
                    }

                    div {
                        font-size: 13px;
                        color: #666;

                        i {
                            display: inline-block;
                            width: 12px;
                            height: 8px;
                            margin-right: 4px;
                            background-image: url(../assets/img/index_icon_2x.png);
                            background-size: 166px 97px;
                        }
                    }
                }

                .right {
                    button {
                        background-color: transparent;
                        color: #ccc;
                    }
                }
            }
        }
    }
}


</style>
